<?php
class cart extends Frontend_Controller {
	public function __construct(){
            parent::__construct();
			$this->load->model('frontend/product_model');
	}
    
	function index(){
		$cache_html = '';
		echo "<pre>";print_r($_SESSION['cart_items']);
        if($this->enable_cache)
            $this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
        echo $cache_html;
        //echo $c=$this->load->view('frontend/home/01_home',$this->frontend_data,true);
	}
	
    
	
	function addtocart(){
		$R['result']=-1;
		$R['message']='SUCCESS !';
		$params = $this->input->post('item');
		
		if($params){
			if(false && isset($_SESSION['cart_items'][$params['id']])){
				if(
					$_SESSION['cart_items'][$params['id']]->product_amount>
					($_SESSION['cart_items'][$params['id']]->quantity+(int)$params['quantity'])
				){
					$_SESSION['cart_items'][$params['id']]->quantity+=(int)$params['quantity'];
					if(isset($_SESSION['cart_items'][$params['id']]->order_detail[$params['size']][$params['color']]))
						$_SESSION['cart_items'][$params['id']]->order_detail[$params['size']][$params['color']]+=(int)$params['quantity'];
					else
						$_SESSION['cart_items'][$params['id']]->order_detail[$params['size']][$params['color']]=(int)$params['quantity'];
					$R['result']=1;
					$R['message']='ADD TO CART SUCCESS !';
				}else{
					$R['result']=-1;
					$R['message']='MAXIMUM QUANTITY !';
				}
			}else{
				$item = $this->product_model->getProductById($params['id']);
				if($item){
					if($item->product_amount>(int)$params['quantity']){
						$item->quantity=(int)$params['quantity'];
						$item->order_detail= null;
						$item->order_detail[$params['size']][$params['color']] = (int)$params['quantity'];
						$_SESSION['cart_items'][$item->product_id] = $item;
						$R['result']=1;
						$R['message']='ADD TO CART SUCCESS !';
					}else{
						$R['result']=-1;
						$R['message']='MAXIMUM QUANTITY !';
					}
				}else{
					$R['result']=-1;
					$R['message']='PRODUCT DOEST EXIST !';
				}
			}
		}else{
			$R['result']=-1;
			$R['message']='DATA INVALID !';
		}
		echo json_encode($R);
	}
	
}
?>